// pages/goods/list.js
const {
    PostNamLike,
    PostGoodsList
} = require('../../http/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        namLike: '', //传过来搜索内容
        myData: [], //商品数据
        listData: [], //模糊搜索
        orderBy: "priceUp", //价格排序 
        flag: true, //辨别价格升序降序
        tabsIndex: 0, //tabs参数
        status: true, //结构状态
    },
    // 价格排序
    price(e) {
        const {
            index
        } = e.currentTarget.dataset
        this.setData({
            flag: !this.data.flag,
            tabsIndex: index
        })
        if (this.data.flag == true) {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: this.data.orderBy
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        } else {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: "priceDown"
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        }
    },
    //销量
    orders(e) {
        const {
            index
        } = e.currentTarget.dataset
        this.setData({
            flag: !this.data.flag,
            tabsIndex: index
        })
        if (this.data.flag == true) {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: "ordersUp"
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        } else {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: "ordersDown"
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        }
    },
    // 新品
    added(e) {
        const {
            index
        } = e.currentTarget.dataset
        this.setData({
            flag: !this.data.flag,
            tabsIndex: index
        })
        if (this.data.flag == true) {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: "addedUp"
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        } else {
            PostNamLike({
                nameLike: this.data.namLike,
                orderBy: "addedDown"
            }).then(res => {
                console.log(res);
                this.setData({
                    listData: res.data.result
                })
            })
        }
    },
    //综合
    zong(e) {
        const {
            index
        } = e.currentTarget.dataset
        this.setData({
            flag: !this.data.flag,
            tabsIndex: index
        })
        PostNamLike({
            nameLike: this.data.namLike
        }).then(res => {
            console.log(res);
            let data1 = res.data.result
            let data2 = data1.filter(ele => ele.name.includes(this.data.namLike))
            this.setData({
                listData: data2
            })
        })
    },
    //图标排序
    icc() {
        this.setData({
            status: !this.data.status
        })
    },
    //搜索
    inp(e) {
        this.setData({
            namLike: e.detail
        })
        PostNamLike({
            nameLike: this.data.namLike
        }).then(res => {
            console.log(res);
            let data1 = res.data.result
            let data2 = data1.filter(ele => ele.name.includes(this.data.namLike))
            this.setData({
                listData: data2
            })

        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.setData({
            namLike: options.name
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        PostNamLike({
            nameLike: this.data.namLike
        }).then(res => {
            console.log(res);

            let data1 = res.data.result
            let data2 = data1.filter(ele => ele.name.includes(this.data.namLike))
            this.setData({
                listData: data2
            })

        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})